<template>
  <div class="app-container">
    <el-form :inline="true">
      <el-form-item label="品牌名称">
        <el-input
          v-model="queryParams.brandName"
          placeholder="请输入品牌名称"
          clearable
          size="small"
          @keyup.enter.native="getBrandList"
        />
      </el-form-item>

      <el-form-item label="状态">
        <el-select v-model="queryParams.status" placeholder="分类状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getBrandList"
          v-hasPermi="['product:brand:query']"
        >搜索
        </el-button>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:brand:add']"
        >新增
        </el-button>
        <el-button type="warning" icon="el-icon-download" size="mini" v-hasPermi="['product:brand:export']"
                   @click="handleExport"
        >导出
        </el-button>
      </el-form-item>
    </el-form>
    <el-table border :data="productBrandList" v-loading="loading">
      <el-table-column prop="brandName" label="品牌名称" min-width="80px" align="center" :show-overflow-tooltip="true"/>
      <el-table-column prop="company" label="所属公司" min-width="200px" align="center" :show-overflow-tooltip="true"/>
      <el-table-column prop="assistUserName" label="联系人" align="center"/>
      <el-table-column prop="assistPhone" label="联系电话" align="center"/>
      <el-table-column prop="status" label="状态" :formatter="statusFormat" width="50" align="center"/>
      <el-table-column prop="remark" label="备注" v-if="false"/>
      <el-table-column prop="createTime" label="创建时间" width="180" align="center"/>
      <el-table-column v-if="false" prop="deptName" label="组织名称" width="150" :show-overflow-tooltip="true"/>
      <el-table-column prop="orderNum" label="排序" width="50" align="center" v-if="false"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
                     v-hasPermi="['product:brand:edit']"
          >修改
          </el-button>
          <el-button size="mini"
                     type="text"
                     icon="el-icon-delete"
                     @click="handleDelete(scope.row)"
                     v-hasPermi="['product:brand:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getBrandList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :before-close="closeDialogConfirm">
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="品牌名称" prop="brandName">
              <el-input v-model="form.brandName" placeholder="请输入品牌名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属公司" prop="company">
              <el-input v-model="form.company" placeholder="请输入公司名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="assistUserName">
              <el-input v-model="form.assistUserName" placeholder="请输入联系人"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="assistPhone">
              <el-input v-model="form.assistPhone" placeholder="请输入联系电话"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属组织" prop="deptId">
              <treeselect v-model="form.deptId" :options="deptTreeOptions" :show-count="true"
                          placeholder="请选择归属组织" disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{ dict.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="orderNum">
              <el-input-number v-model="form.orderNum" controls-position="right" :min="0"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea"/>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addBrand, deleteBrand, exportBrandList, getBrandList, updateBrand } from '@/api/system/productManage'
import { getOrderNumOfList } from '@/utils/ruoyi'
import { deptTreeSelect } from '@/api/system/dept'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'index',
  components: { Treeselect },
  data() {
    return {
      loading: true,
      productBrandList: [],
      deptTreeOptions: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        brandName: undefined,
        status: undefined
      },
      // 状态数据字典
      statusOptions: [],
      title: '',
      open: false,
      form: {
        brandName: undefined,
        company: undefined,
        assistUserName: undefined,
        assistPhone: undefined,
        deptId: undefined,
        status: '0',
        orderNum: 1,
        remark: undefined
      },
      // 操作类型 add/update
      operationType: '',
      rules: {
        brandName: [
          { required: true, message: '品牌名称不能为空', trigger: 'blur' }
        ],
        deptId: [
          { required: true, message: '归属组织不能为空', trigger: 'blur' }
        ],
        assistPhone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: '请输入正确的手机号', trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getBrandList()
    this.getDeptTreeOptions()
    this.getDictDataByType('sys_normal_disable').then(response => {
      this.statusOptions = response.data
    })
  },
  methods: {
    getBrandList() {
      this.loading = true
      getBrandList(this.queryParams).then(response => {
        this.productBrandList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 查询部门下拉树结构 */
    getDeptTreeOptions() {
      deptTreeSelect().then(response => {
        this.deptTreeOptions = response.data
      })
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    /** 修改品牌 */
    handleUpdate(row) {
      this.reset()
      this.operationType = 'update'
      this.open = true
      this.title = '修改品牌'
      let selectedBrand = this.productBrandList.find(item => {
        return item.id === row.id
      })
      if (selectedBrand) {
        this.form = { ...selectedBrand }
      }
    },
    /** 删除品牌 */
    handleDelete(row) {
      this.$confirm('是否删除品牌"' + row.brandName + '"?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteBrand(row.id).then(()=>{
          this.getBrandList()
          this.msgSuccess('删除成功')
        })
      }).then(() => {
      })
    },
    /** 添加品牌 */
    handleAdd() {
      this.reset()
      this.operationType = 'add'
      this.open = true
      this.title = '添加品牌'
      this.form.deptId = 100
      this.form.orderNum = getOrderNumOfList(this.productBrandList)
    },
    /** 提交表单 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.operationType === 'add') {
            addBrand(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('添加成功')
                this.open = false
                this.getBrandList()
              }
            })
          }
          if (this.operationType === 'update') {
            updateBrand(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getBrandList()
              }
            })
          }
        }
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        brandName: undefined,
        company: undefined,
        assistUserName: undefined,
        assistPhone: undefined,
        deptId: undefined,
        status: '0',
        orderNum: 1,
        remark: undefined
      }
      this.resetForm('form')
    },

    /**
     * 对话框确认关闭
     */
    closeDialogConfirm() {
      this.$confirm('确认关闭?').then(() => {
        this.open = false
      }).catch(() => {
      })
    },

    /** 导出按钮操作 */
    handleExport() {
      this.$confirm('是否确认导出?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return exportBrandList(this.queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    }
  }
}
</script>

<style scoped>

</style>
